package
{
	import org.flixel.FlxSprite;
	
	public class SourceForge extends FlxSprite {
		
		protected var earthSources:int;
		protected var lightSources:int;
		protected var waterSources:int;
		protected var windSources:int;
		
		protected var currentElement:int;
		protected var newElement:int;
		
		protected var transformationAnimator:FlxSprite;
		
		//Creates a source forge tile at location X, Y
		public function SourceForge(X:int, Y:int, tranAnim:FlxSprite)
		{
			super(X, Y, null);
			
			transformationAnimator = tranAnim;
			transformationAnimator.addAnimation("silent", [0], 1);
			transformationAnimator.addAnimation("transform", [0,1,2,3,4,5,6,6,6,6,6,6,6,6,6,6], 8);
			transformationAnimator.addAnimationCallback(stopTransform);
			//add anims
			
			earthSources = 0;
			lightSources = 0;
			waterSources = 0;
			windSources = 0;
			currentElement = 0;
			newElement = 0;
			addAnimation("active", [0,1,2,3,4,5,6,7], 7);
			addAnimation("normal", [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], 3);
			play("normal");
			loadGraphic(Res.sfNormalAnim, true, false, 46, 46, false);
		}
		
		/**
		 * updates the state of this source forge 
		 */
		public override function update():void
		{  
			super.update();
			newElement = 0;
			if (earthSources > 0) {
				newElement += Elemental.EARTH;
			}
			if (lightSources > 0) {
				newElement +=  Elemental.LIGHT;
			}
			if (waterSources > 0) {
				newElement += Elemental.WATER;
			}
			if (windSources > 0) {
				newElement += Elemental.WIND;
			}
			if (newElement != currentElement) {
				currentElement = newElement;
				updateImage();
			}
		}
		
		/****************************
		 * stubz
		 ****************************/
		/**
		 * Adds an incoming source type to the source forge. 
		 * element must be 1: Earth Source, 2: Light Source, 4: Water Source, or 8: Wind Source
		 */
		public function addSource(element:int):void {
			if (element == 1) {
				earthSources++;
			} else if (element == 2) {
				lightSources++;
			} else if (element == 4) {
				waterSources++;
			} else if (element == 8) {
				windSources++;
			}
		}
		
		/**
		 * Removes an incoming source type from the source forge. If the source forge does not have
		 * the given element already incoming, nothing will change.
	 	 * element must be 1: Earth Source, 2: Light Source, 4: Water Source, or 8: Wind Source
		 */
		public function removeSource(element:int):void {
			if (element == 1) {
				earthSources = Math.max(0, earthSources - 1);
			} else if (element == 2) {
				lightSources = Math.max(0, lightSources - 1);
			} else if (element == 4) {
				waterSources = Math.max(0, waterSources - 1);
			} else if (element == 8) {
				windSources  = Math.max(0, windSources - 1);
			}
		}
		
		/**
		 * Returns the current element value of the source forge.
		 */
		public function get element():int {
			return currentElement;
		}
		
		/**
		 * Returns wether or not this source forge is lit and ready to transform the player.
		 */
		public function isLit():Boolean { 
			if (earthSources + lightSources + waterSources + windSources > 0) 
				return true; 
			else 
				return false;
		}
		
		/**
		 * changes the image of the source forge to the current element
		 */
		private function updateImage():void {
			if (currentElement == Elemental.NORMAL) {
				play("normal");
				loadGraphic(Res.sfNormalAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.EARTH) {
				play("active");
				loadGraphic(Res.sfEarthAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.LIGHT) {
				play("active");
				loadGraphic(Res.sfLightAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.WATER) {
				play("active");
				loadGraphic(Res.sfWaterAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.WIND) {
				play("active");
				loadGraphic(Res.sfWindAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.SAND) {
				play("active");
				loadGraphic(Res.sfSandAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.MUD) {
				play("active");
				loadGraphic(Res.sfMudAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.STEAM) {
				play("active");
				loadGraphic(Res.sfSteamAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.LIGHTNING) {
				play("active");
				loadGraphic(Res.sfLightningAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.FIRE) {
				play("active");
				loadGraphic(Res.sfFireAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.ICE) {
				play("active");
				loadGraphic(Res.sfIceAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.WOOD) {
				play("active");
				loadGraphic(Res.sfWoodAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.METAL) {
				play("active");
				loadGraphic(Res.sfMetalAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.GLASS) {
				play("active");
				loadGraphic(Res.sfGlassAnim, true, false, 46, 46, false);
			} else if (currentElement == Elemental.RAINBOW) {
				play("active");
				loadGraphic(Res.sfRainbowAnim, true, false, 46, 46, false);
			} else {
				play("active");
				loadGraphic(Res.sfVoidAnim, true, false, 46, 46, false);
			}
		}
		
		public function transformationAnim():void {
			transformationAnimator.frame = 0;
			switch(currentElement) {
				case Elemental.WATER:
					transformationAnimator.loadGraphic(Res.waterTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.LIGHT:
					transformationAnimator.loadGraphic(Res.lightTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.WIND:
					transformationAnimator.loadGraphic(Res.windTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.EARTH:
					transformationAnimator.loadGraphic(Res.earthTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.MUD:
					transformationAnimator.loadGraphic(Res.mudTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.SAND:
					transformationAnimator.loadGraphic(Res.sandTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.ICE:
					transformationAnimator.loadGraphic(Res.iceTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.RAINBOW:
					transformationAnimator.loadGraphic(Res.rainbowTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.LIGHTNING:
					transformationAnimator.loadGraphic(Res.lightningTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.METAL:
					transformationAnimator.loadGraphic(Res.metalTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.FIRE:
					transformationAnimator.loadGraphic(Res.fireTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.STEAM:
					transformationAnimator.loadGraphic(Res.steamTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.WOOD:
					transformationAnimator.loadGraphic(Res.woodTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.GLASS:
					transformationAnimator.loadGraphic(Res.glassTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.VOID:
					transformationAnimator.loadGraphic(Res.voidTransformAnim, true, false, 92, 92, false);
					transformationAnimator.play("transform");
					break;
				case Elemental.NORMAL:
				default:
					transformationAnimator.loadGraphic(Res.sfBoundingBox, false, false, 20, 20, false);
					transformationAnimator.play("silent");
					break;
			}
		}
		
		private function stopTransform(name:String, frameNumber:int, frameIndex:int):void {
			if (frameNumber > 10) {
				transformationAnimator.alpha -= .2;
			}
			if (transformationAnimator.finished) {
				transformationAnimator.loadGraphic(Res.sfBoundingBox, false, false, 20, 20, false);
				transformationAnimator.play("silent");
				transformationAnimator.alpha = 1;
			}
		}
	}
}